package com.example.mydataaccesssample;

import com.example.mydataaccesssample.data.CDbManager;

import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ActSqlite extends Activity {


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.actsqlite);
		InitialComponent();
	}
	OnClickListener btnNew_click=new OnClickListener(){
		public void onClick(View arg0) {
			
			ContentValues data =new ContentValues();
			data.put("fId", txtId.getText().toString());
			data.put("fSubject", txtSubject.getText().toString());
			data.put("fDate", txtDate.getText().toString());
			data.put("fTime", txtTime.getText().toString());
			data.put("fLocation", txtLocation.getText().toString());
			
			CDbManager db=new CDbManager(ActSqlite.this);
			db.newData("tMeeting", data);
			
			Toast.makeText(ActSqlite.this, "新增資料成功", Toast.LENGTH_SHORT).show();
		
			
	}};
	
	OnClickListener btnDelete_click=new OnClickListener(){
		public void onClick(View arg0) {
			CDbManager db=new CDbManager(ActSqlite.this);
			db.deleteData("tMeeting", strPK);
			Toast.makeText(ActSqlite.this, "資料刪除成功", Toast.LENGTH_SHORT).show();
			
			txtId.setText("");
			txtSubject.setText("");
			txtDate.setText("");
			txtTime.setText("");
			txtLocation.setText("");
	}};	
	OnClickListener btnEdit_click=new OnClickListener(){
		public void onClick(View arg0) {

			
			CDbManager db=new CDbManager(ActSqlite.this);
			Cursor table=db.GetBySql("SELECT * FROM tMeeting WHERE _id="+strPK);
			System.out.println("id="+strPK);
			
			String db_txtId = new String();
			String db_txtSubject = new String();
			String db_txtDate = new String();
			String db_txtTime = new String();
			String db_txtLocation = new String();
			
			if(table.getCount()>0){
				//把指標移動到第一格
				table.moveToFirst();
				//抓取表格的欄位
				db_txtId = table.getString(1);
				db_txtSubject =  table.getString(2);
				db_txtDate = table.getString(3);
				db_txtTime = table.getString(4);
				db_txtLocation = table.getString(5);
				
			}
			
			if(txtId.getText().toString()!= db_txtId ||
					txtSubject.getText().toString()!=db_txtSubject ||
					txtDate.getText().toString()!=db_txtDate ||
					txtTime.getText().toString()!=db_txtTime ||
					txtLocation.getText().toString()!=db_txtLocation)
			{
				//這邊可以簡單去寫，不管有沒有修改到，點到就更新
				ContentValues valueData = new ContentValues();
				valueData.put("fid", txtId.getText().toString());
				valueData.put("fSubject", txtSubject.getText().toString());
				valueData.put("fDate", txtDate.getText().toString());
				valueData.put("fTime", txtTime.getText().toString());
				valueData.put("fLocation", txtLocation.getText().toString());	
				db.updateData("tMeeting", valueData, strPK);
				
				Toast.makeText(ActSqlite.this, "資料更新成功", Toast.LENGTH_SHORT).show();
			}
		
	
		
	}};
	
	OnClickListener btnBrowse_click=new OnClickListener(){
		public void onClick(View arg0) {
			Intent intent =new Intent(ActSqlite.this,ActList.class);
			startActivityForResult(intent,122);
	}};
	//取得PK
	String strPK;
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(data==null)
			return;
		if(data.getExtras()==null)
			return;

		//取得接收回來的KEY
		strPK=String.valueOf(data.getExtras().getLong("KK"));	
		CDbManager db=new CDbManager(this);
		//查詢目前的資料
		Cursor table=db.GetBySql("SELECT * FROM tMeeting WHERE _id="+strPK);
		if(table.getCount()>0){
			//把指標移動到第一格
			table.moveToFirst();
			//抓取表格的欄位
			txtId.setText(table.getString(1));
			txtSubject.setText(table.getString(2));
			txtDate.setText(table.getString(3));
			txtTime.setText(table.getString(4));
			txtLocation.setText(table.getString(5));
		}
	}
	
	
	private void InitialComponent() {

		btnNew=(Button)findViewById(R.id.btnNew);
		btnNew.setOnClickListener(btnNew_click);
		
		btnDelete=(Button)findViewById(R.id.btnDelete);
		btnDelete.setOnClickListener(btnDelete_click);
		
		btnEdit=(Button)findViewById(R.id.btnEdit);
		btnEdit.setOnClickListener(btnEdit_click);
		
		
		btnBrowse=(Button)findViewById(R.id.btnBrowse);
		btnBrowse.setOnClickListener(btnBrowse_click);
		
		txtId=(EditText)findViewById(R.id.txtId);
		txtSubject=(EditText)findViewById(R.id.txtSubject);
		txtDate=(EditText)findViewById(R.id.txtDate);
		txtTime=(EditText)findViewById(R.id.txtTime);
		txtLocation=(EditText)findViewById(R.id.txtLocation);
	}
   

	Button btnNew=null;
	Button btnDelete=null;
	Button btnEdit=null;
	Button btnBrowse=null;

	
	EditText txtId=null;
	EditText txtSubject=null;
	EditText txtDate=null;
	EditText txtTime=null;
	EditText txtLocation=null;
	
	
	

}
